//数据库数据模型（存放分类信息的模型 （表）），哪里需要直接引用
const mongoose = require('mongoose')

const schema = new mongoose.Schema({
  name: { type: String },
  //添加父级分类字段(mongoose.SchemaTypes.ObjectId表示是数据库里面的对象id，方便查询整条数据，ref表示关联哪个模型（表）)
  parent: { type: mongoose.SchemaTypes.ObjectId, ref: 'Category' }
})

//子分类
schema.virtual('children', {
  localField: '_id',
  foreignField: 'parent',
  justOne: false,
  ref: 'Category'
})

schema.virtual('newsList', {
  localField: '_id',
  foreignField: 'categories',
  justOne: false,
  ref: 'Article'
})
module.exports = mongoose.model('Category', schema)